#include "incs.h"

#include "OgreApplication.h"
#include "GameManager.h"
#include "MiscOgre.h"
#include "StateRegistry.h"

#include "MenuScreenState.h"


void MenuScreenState::createScene()
{
	Ogre::Root * root = Ogre::Root::getSingletonPtr();
	mSceneMgr = root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, "Menu_SceneManager");

	mCamera = mSceneMgr->createCamera("Menu_Camera");
	mViewport = root->getAutoCreatedWindow()->addViewport(mCamera);
	mCamera->setFarClipDistance(10000);
	mCamera->setNearClipDistance(0.01);
	mSceneMgr->setAmbientLight( Ogre::ColourValue( 0.5, 0.5, 0.5 ) );
//	mViewport->setBackgroundColour( Ogre::ColourValue(0, 0.1, 0.1));


	Ogre::Entity * robEnt  = mSceneMgr->createEntity("RobotL", "robot.mesh");
	Ogre::SceneNode * robNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(
		"RobotLNode", Ogre::Vector3(0, 0, -100) );
	robNode->setScale(.5, .5, .5);
	robNode->attachObject(robEnt);
	robNode->yaw(Ogre::Degree(40));
	robNode->translate(0, 0, -10, Ogre::Node::TS_LOCAL);


	Ogre::Entity * isdEnt = mSceneMgr->createEntity("tie", "tie.mesh");
	Ogre::SceneNode * isdNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(
		"tie", Ogre::Vector3(0, 0, -100) );
	isdNode->attachObject(isdEnt);
	isdNode->setScale(10, 10, 10);
	isdNode->translate(0, 100, -10, Ogre::Node::TS_LOCAL);



	mSceneMgr->setWorldGeometry("terrain.cfg");
	mSceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);

	robNode->translate(0, 1000, 0, Ogre::Node::TS_LOCAL);

	robNode->attachObject(mCamera);

	
	mCamera->setPosition(Ogre::Vector3(0, -912, 1200));
	mCamera->yaw(Ogre::Degree(180));


	//SetupCEGUI(root->getAutoCreatedWindow(), mSceneMgr);

}

bool MenuScreenState::OnFrameStarted( Ogre::FrameEvent const & evt )
{

	// Update FPS stats.
	Ogre::RenderWindow * renderWin = OgreApp.getRenderWindow();
	//updateStats(renderWin);

	// Update camera control.
	BaseGameManager & gm = GetGameManager();
	OIS::MouseState const & ms = OgreApp.getMouse()->getMouseState();
	mCameraControl->Update(this, evt.timeSinceLastFrame, gm.GetKeysState(), ms, 
		mSceneMgr); 


	return true;
}

void MenuScreenState::OnKeyPressed( OIS::KeyEvent const & e )
{
	//CEGUI::System *sys = CEGUI::System::getSingletonPtr();
	//sys->injectKeyDown(e.key);
	//sys->injectChar(e.text);


	if(e.key == OIS::KC_ESCAPE)
	{
		PopState();
		GetGameManager().ShouldExit(true);
	}
}

void MenuScreenState::OnKeyReleased( OIS::KeyEvent const & e )
{
	//CEGUI::System::getSingleton().injectKeyUp(e.key);
}

bool MenuScreenState::OnMousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id )
{
	//CEGUI::System::getSingleton().injectMouseButtonDown(
	//	MiscOgre::OISButtonToCEGUI(id));
	return true;
}

bool MenuScreenState::OnMouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id )
{
	//CEGUI::System::getSingleton().injectMouseButtonUp(
	//	MiscOgre::OISButtonToCEGUI(id));
	return true;
}

bool MenuScreenState::OnMouseMoved( const OIS::MouseEvent &e )
{
	//CEGUI::System::getSingleton().injectMouseMove(e.state.X.rel, e.state.Y.rel);
	return true;
}

void MenuScreenState::createGUI()
{

}

void MenuScreenState::OnEnter()
{
	createScene();
	createGUI();

	mCameraControl.reset(new CameraControl(mCamera));

	//enableDebugOverlay(true);
}

void MenuScreenState::OnExit()
{
	//CleanupCEGUI();
	Ogre::Root * root = Ogre::Root::getSingletonPtr();
	root->destroySceneManager(mSceneMgr);
	OgreApp.getRenderWindow()->removeAllViewports();
}

MenuScreenState::MenuScreenState( BaseGameManager & gameManager ) : 
IGameState(gameManager), 
mCamera(0), mSceneMgr(0), mViewport(0)
{

}

const char * MenuScreenState::GetStateName()
{
	return "LoginScreenState";
}